<?php

class AdminExportController extends AdminController {

	public function getIndex($id = false)
	{
		try{
			if($id != false){
				$workbook = new PHPExcel;

				$model     = new Order;
				$order     = Order::find($id);
				$customer  = Customer::find($order->customer);

				$customer_payment_model = new CustomerPayment;
				$dept      = $customer_payment_model->getDept($customer->id, $order->date, $order->id);
				$dept      -= $customer_payment_model->getPayment($customer->id, $order->date, $order->id);
				$totalpay  = $customer_payment_model->getOrderPayment($order->id);
				$refund    = $customer_payment_model->getOrderRefund($order->id);
				
				$remaining = $order->total + $dept - $totalpay - $refund;

				$order_detail = new OrderDetail;
				$items = $order_detail->getOrderItemsToPrint($order);

				$sheet = $workbook->getActiveSheet();
		        $sheet->getStyle("A1")->getFont()->setBold(true);
		        $sheet->getStyle("A6:F6")->getFont()->setBold(true);
		        $sheet->getStyle("A1:G100")->getFont()->setName("Times New Roman")->setSize(12);
		        $sheet->getStyle("A1")->getFont()->setBold(true)->setItalic(true);
		        $sheet->getStyle("E1:F2")->getFont()->setName("Times New Roman")->setSize(16)->setBold(true);
		        $sheet->getStyle("A5")->getFont()->setItalic(true)->setSize(14);
		        
		        $sheet->getStyle("E1:F2")
		                ->getAlignment()
		                ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
		                ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		        
		        $sheet->getStyle('A5')
		                ->getAlignment()
		                ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		                
		        $sheet->getStyle('A6:F6')
		                ->getAlignment()
		                ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		        
		        $option = array('fill' => array(
		                                'type' => PHPExcel_Style_Fill::FILL_SOLID,
		                                'startcolor' => array(
		                                    'rgb' => 'FFFF00',
		                                ),
		                            ),);
		        $sheet->getStyle("A5")->applyFromArray($option);
		        $sheet->freezePane('A7');
		        
		        $sheet->getDefaultRowDimension()->setRowHeight(20);
		        $sheet->getColumnDimension("A")->setWidth(45);
		        $sheet->getColumnDimension("F")->setWidth(17);
		        
		        $sheet->getStyle('A5:F6')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		        
		        $sheet->getStyle('D7:H100')
		                ->getAlignment()
		                ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		        
		        $sheet->setCellValue('A1', 'CÔNG TY TNHH SX-TM MP HÂN VY');
		        $sheet->setCellValue('A2', 'Địa chỉ: 35 Tam Hà, Phường Tam Phú, Quận Thủ Đức, TPHCM');
		        $sheet->mergeCells('E1:F2');
		        $sheet->setCellValue('E1', $customer->name);
		        $sheet->setCellValue('A3', 'Điện Thoại: (84-8) 39861237-Fax: (84-8) 37296239');
		        $sheet->mergeCells('E3:F3');
		        $sheet->setCellValue('E3', "Ngày: " . date("d-m-Y", strtotime($order->date)));
		        $sheet->setCellValue('A4', 'Điện Thoại tư vấn: 0973.479.839-0938.798.876');
		        $sheet->mergeCells('E4:F4');
		        $sheet->setCellValue('E4', 'HĐ số: ' . $order->order_number);
		        $sheet->mergeCells('A5:F5');
		        $sheet->setCellValue('A5', 'HÓA ĐƠN GỞI HÀNG');
		        
		        $sheet->setCellValue('A6', 'TÊN SẢN PHẨM');
		        $sheet->setCellValue('B6', 'NET');
		        $sheet->setCellValue('C6', 'MH');
		        $sheet->setCellValue('D6', 'SL');
		        $sheet->setCellValue('E6', 'ĐƠN GIÁ');
		        $sheet->setCellValue('F6', 'THÀNH TIỀN');

		        $i = 7;

		        foreach($items as $item){
		        	$sheet->setCellValue('A' . $i, $item->name);
	                $sheet->setCellValue('B' . $i, $item->net);
	                $sheet->setCellValue('C' . $i, strtoupper($item->brand));
	                $sheet->setCellValue('D' . $i, $item->quantity);
	                $sheet->setCellValue('E' . $i, $item->price);
	                $sheet->setCellValue('F' . $i, $item->price * $item->quantity);
	                
	                $sheet->getStyle('E' . $i)->getNumberFormat()->setFormatCode("#,##0.##0");
	                $sheet->getStyle('F' . $i)->getNumberFormat()->setFormatCode("#,##0.##0");
	                $sheet->getStyle("A$i:F$i")->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
	                $sheet->getStyle("B$i:C$i")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	                    
	                $i ++;
		        }

		        $i += 2;
	            
	            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
	            $sheet->getStyle("A$i")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	            $sheet->setCellValue("A$i", date("d-m-Y", strtotime($order->date)));
	            $sheet->setCellValue("C$i", "TỔNG TOA HÔM NAY");
	            $sheet->setCellValue("F$i", $order->subtotal);
	            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0.##0");
	            
	            $i += 1;
	            
	            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
	            $sheet->setCellValue("C$i", "THANH TOÁN");
	            $sheet->setCellValue("F$i", $totalpay);
	            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0.##0");
	            
	            $i += 1;
	            
	            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
	            $sheet->setCellValue("C$i", "NỢ CŨ");
	            $sheet->setCellValue("F$i", $dept);
	            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0.##0");
	            
	            $i += 1;
	            
	            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
	            $sheet->setCellValue("C$i", "TRẢ HÀNG");
	            $sheet->setCellValue("F$i", $refund);
	            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0.##0");
	            
	            $i += 1;
	            
	            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
	            $sheet->getStyle("A$i")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	            $sheet->setCellValue("A$i", "THANH KHƯƠNG");
	            $sheet->setCellValue("C$i", "TỔNG CÒN LẠI");
	            $sheet->setCellValue("F$i", $remaining);
	            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0.##0");

	            $name = StringFormat::format($customer->name);
	            $name .= date(" d-m-Y", strtotime($order->date));

		        $writer = new PHPExcel_Writer_Excel5($workbook);
		        $writer->save("d:/" . $name . ".xls");

		        return Redirect::to('admin/order/list/' . $customer->type);

		        $this->layout->content = View::make('admin.index', array('name' => 'Nhật Đỗ'));
	        }else{
				$this->layout->content = View::make('admin.index', array('name' => 'Không tìm thấy hóa đơn'));
	        }
        }catch(Exception $e){
        	$this->layout->content = View::make('admin.index', array('name' => 'Không ghi được file'));
        }

        $this->layout->title = trans("export.excel");
	}
}
